
package com.xebest.web.service.impl;

import com.xebest.common.enums.erp.order.VenConfirmSendNumEnum;
import com.xebest.common.enums.erp.order.VenInquiryStatusEnum;
import com.xebest.common.enums.erp.order.VenInquiryTypeEnum;
import com.xebest.common.enums.erp.order.VenOrderSourceEnum;
import com.xebest.common.util.XeBaseUtil;
import com.xebest.util.SysSetBeanUtils;
import com.xebest.web.controller.export.*;
import com.xebest.web.form.XeVenInquirySheetForm;
import com.xebest.web.form.XeVenOrderForm;
import com.xebest.web.mapper.read.XeVenOrderCenterEPReadMapper;
import com.xebest.web.mapper.read.XeVenOrderReadMapper;
import com.xebest.web.service.XeVenOrderCenterExportService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 处理订单中心导出
 * @desc: xebest-erp  
 * @author: 赵盼飞  
 * @createTime: 2017年6月23日 上午10:33:24  
 * @history:  
 * @version: v1.0
 */
@Service
public class XeVenOrderCenterExportServiceImpl implements XeVenOrderCenterExportService {

	@Autowired
	private XeVenOrderCenterEPReadMapper orderCenterEPReadMapper;  //查询订单业务操作
//    @Autowired
//    private XeVenOrderReadMapper xeVenOrderReadMapper;  //查询订单业务操作
	/**
	 * 处理订单中心导出
	 */
	@Override
	public List<XeVenOrderExportDTO> queryExportOrderList(Map<String,Object> paramMap) {
		List<XeVenOrderExportDTO> orderExportList = new ArrayList<XeVenOrderExportDTO>();
		orderExportList = orderCenterEPReadMapper.queryExportOrderList(paramMap);
//		XeVenOrderForm orderForm = (XeVenOrderForm) paramMap.get("orderForm");
//		for(XeVenOrderExportDTO orderView : orderExportList){
//			orderForm.setOrderId(orderView.getOrderId());
//	        Map<String, Object> resultMap = xeVenOrderReadMapper.queryRefundAmount(orderForm);
//	        if (null != resultMap) {
//	            //退款金额
//	            if (resultMap.containsKey("refundAmount") && !StringUtils.isEmpty(resultMap.get("refundAmount").toString())) {
//	                orderView.setRefundAmount(new BigDecimal(resultMap.get("refundAmount").toString()));
//	            }
//	            if (orderView.getConfirmSendNum().equals(VenConfirmSendNumEnum.YES.getValue())) {
//	                //有实发差异
//	                if (resultMap.containsKey("sfRefundAmount") && !StringUtils.isEmpty(resultMap.get("sfRefundAmount").toString())) {
//	                    //差异金额
//	                    orderView.setSfRefundAmount(new BigDecimal(resultMap.get("sfRefundAmount").toString()));
//	                    //差异运费金额
//	                    orderView.setFreRefundAmount(orderView.getDifferenceAmount().subtract(orderView.getSfRefundAmount()));
//	                    //实发金额
//	                    orderView.setActualAmout(orderView.getEpayReceAmt().subtract(orderView.getFreRefundAmount()));
//	                    //实发运费
//	                    orderView.setActualFreAmout(orderView.getFreAmount().subtract(orderView.getActualFreAmout()));
//	                } else {
//	                    //差异金额
//	                    orderView.setSfRefundAmount(new BigDecimal("0"));
//	                    //差异运费金额
//	                    orderView.setFreRefundAmount(new BigDecimal("0"));
//	                    //实发金额
//	                    orderView.setActualAmout(orderView.getEpayReceAmt());
//	                    //实发运费
//	                    orderView.setActualFreAmout(orderView.getFreAmount());
//	                }
//	            }
//	        }
//		}
		return orderExportList;
	}

	/**
	 * 导出订单明细
	 */
	@Override
	public List<XeVenOrderDetailExportDTO> queryExportOrderDetailList(Map<String,Object> paramMap) {
		List<XeVenOrderDetailExportDTO> detailList = new ArrayList<XeVenOrderDetailExportDTO>();
		detailList = orderCenterEPReadMapper.queryExportOrderDetailList(paramMap);
//		detailList.stream().forEach(detailExport -> {
		int size = detailList.size();
		for (int i = 0; i < size; i++) {
			XeVenOrderDetailExportDTO detailExport = detailList.get(i);
			//数字字典转对应字符串
			if (org.apache.commons.lang.StringUtils.isNotBlank(detailExport.getCaAreaId())) {
				String orgAreaName = XeBaseUtil.baseUtil.getOrgAreaName(Integer.parseInt(detailExport.getCaAreaId().trim()));
				detailExport.setCaArea(orgAreaName);
			} else if (org.apache.commons.lang.StringUtils.isNotBlank(detailExport.getCaCityId())){
				String caCityName = XeBaseUtil.baseUtil.getAreaName(Integer.parseInt(detailExport.getCaCityId().trim()));
				String caProvinceName = XeBaseUtil.baseUtil.getAreaName(Integer.parseInt(detailExport.getCaProvinceId().trim()));
				detailExport.setCaArea(caProvinceName+"-"+caCityName);
			}
			if (org.apache.commons.lang.StringUtils.isNotBlank(detailExport.getSalesmanName())) {
				detailExport.setSalesmanName(detailExport.getSalesmanName() + detailExport.getSalesmanPhone());
			}
			BigDecimal rea = com.xebest.common.util.XeBaseUtil.baseUtil.calSendAmt(detailExport.getPurchaseNum(), 
					detailExport.getRealSendNum(), detailExport.getOrderAmount(), detailExport.getPmtAmount());
			detailExport.setRealSendAmount(rea);
		}
		return detailList;
	}

	/**
	 * 导出询价单列表
	 */
	@Override
	public List<XeVenInquiryOrderExportDTO> queryXeVenInquirySheetExportList(XeVenInquirySheetForm form) {
		List<XeVenInquiryOrderExportDTO> inquiryOrderList = orderCenterEPReadMapper.queryXeVenInquirySheetExportList(form);
		for (XeVenInquiryOrderExportDTO inquiryOrder : inquiryOrderList) {
			//数字字典转对应字符串
			if(!StringUtils.isEmpty(inquiryOrder.getInquirySource())){
				SysSetBeanUtils.codeReversionStr("inquirySource",inquiryOrder,VenOrderSourceEnum.class);
			}
			if(!StringUtils.isEmpty(inquiryOrder.getInquiryType())){
				SysSetBeanUtils.codeReversionStr("inquiryType",inquiryOrder,VenInquiryTypeEnum.class);
			}
			if(!StringUtils.isEmpty(inquiryOrder.getInquiryStatus())){
				SysSetBeanUtils.codeReversionStr("inquiryStatus",inquiryOrder,VenInquiryStatusEnum.class);
			}
		}
		return inquiryOrderList;  
	}

	/**
	 * 获取汇总备货订单维度导出数据
	 * @author: 赵盼飞  
	 * @createTime: 2017年8月31日 上午11:20:36  
	 * @history:  
	 * @param form
	 * @return List<XeVenStockUpOrderExDTO>
	 */
	@Override
	public List<XeVenStockUpOrderExDTO> queryExportStockUpOrderList(Map<String, Object> paramMap) {
		List<XeVenStockUpOrderExDTO> stockUpOrderList = orderCenterEPReadMapper.queryExportStockUpOrderList(paramMap);
		for(XeVenStockUpOrderExDTO stockUpOrder : stockUpOrderList){
			String orgAreaName = XeBaseUtil.baseUtil.getOrgAreaName(Integer.parseInt(stockUpOrder.getCaAreaId()));
			stockUpOrder.setCaAddress(orgAreaName);
		}
		return stockUpOrderList;  
		
	}

	/**
	 * 获取汇总备货订单明细维度导出数据
	 * @author: 赵盼飞  
	 * @createTime: 2017年8月31日 上午11:20:36  
	 * @history:  
	 * @param form
	 * @return List<XeVenStockUpOrderDetailExDTO>
	 */
	@Override
	public List<XeVenStockUpOrderDetailExDTO> queryExportStockUpOrderDetailList(Map<String, Object> paramMap) {
		  
		return orderCenterEPReadMapper.queryExportStockUpOrderDetailList(paramMap);  
		
	}

	/**
	 * 获取汇总备货商品维度导出数据
	 * @author: 赵盼飞  
	 * @createTime: 2017年8月31日 上午11:20:36  
	 * @history:  
	 * @param form
	 * @return List<XeVenStockUpProductExDTO>
	 */
	@Override
	public List<XeVenStockUpProductExDTO> queryExportStockUpProductList(Map<String, Object> paramMap) {
		  
		return orderCenterEPReadMapper.queryExportStockUpProductList(paramMap);  
	}
	
	
}